unit EntradaTransRecordFr;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ComCtrls, DBCtrls, ActnList, Buttons, DBGrids, ExtCtrls, EditBtn,
  MainTransactionRecordFr, DataRecord;

type

  { TEntradaTransRecordFrame }

  TEntradaTransRecordFrame = class(TMainTransactionRecordFrame)
    acrescimoDBEdit: TDBEdit;
    AlterarPrecoAction: TAction;
    BitBtn1: TBitBtn;
    UnidadeEdit: TEdit;
    Label14: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label7: TLabel;
    SalvarPrecoAction: TAction;
    CancelarPrecoAction: TAction;
    AprazoTabSheet: TTabSheet;
    AvistaTabSheet: TTabSheet;
    BuscarFornecedor: TBitBtn;
    BuscarProduto: TBitBtn;
    cancelar: TToolButton;
    CartaoTabSheet: TTabSheet;
    codigoEdit: TEdit;
    ComboPagamento: TDBLookupComboBox;
    data_emissao: TDBEdit;
    data_entrada: TDBEdit;
    data_recebimento1: TDBEdit;
    DBGrid2: TDBGrid;
    DBGrid3: TDBGrid;
    DBGrid4: TDBGrid;
    DBGroupBox1: TDBGroupBox;
    DBGroupBox2: TDBGroupBox;
    DBGroupBox3: TDBGroupBox;
    DBGroupBox5: TDBGroupBox;
    DBGroupBox6: TDBGroupBox;
    DBLookupComboBox1: TDBLookupComboBox;
    DBLookupComboBox2: TDBLookupComboBox;
    DescontoCartaoDBEdit: TDBEdit;
    DescontoChequeDBEdit: TDBEdit;
    descontoDBEdit: TDBEdit;
    descricaoEdit: TEdit;
    DetailDBGrid: TDBGrid;
    DetailDBNavigator: TDBNavigator;
    DetailGroupBox: TGroupBox;
    diaVencimentoCheque2: TDateEdit;
    diaVencimentoEdit: TDateEdit;
    DuplicatasTabSheet: TTabSheet;
    EntradaToolBar: TToolBar;
    FinanceiroTabSheet: TTabSheet;
    freteDBEdit: TDBEdit;
    GerarCheques1: TButton;
    GerarDuplicatasPrazo1: TButton;
    Gravar: TToolButton;
    GroupBox2: TGroupBox;
    id_entrada: TDBEdit;
    id_entrada1: TDBEdit;
    id_fornecedor: TDBEdit;
    id_fornecedor1: TDBEdit;
    Label1: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label2: TLabel;
    Label23: TLabel;
    Label26: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label3: TLabel;
    Label30: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    MasterGroupBox: TGroupBox;
    MasterTabSheet: TTabSheet;
    nome_pessoa: TDBEdit;
    nome_pessoa1: TDBEdit;
    numero_nf: TDBEdit;
    numero_nf1: TDBEdit;
    Pagamento: TPageControl;
    Pagamento1: TPageControl;
    Painel1: TPanel;
    Painel3: TPanel;
    PainelTroco: TPanel;
    Panel11: TPanel;
    Panel12: TPanel;
    Panel13: TPanel;
    Panel14: TPanel;
    Panel16: TPanel;
    Panel17: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    Panel9: TPanel;
    quantidadeEdit: TEdit;
    RecordPageControl: TPageControl;
    RemoverItensProdutos: TSpeedButton;
    serie_nf: TDBEdit;
    StatusCliente: TDBRadioGroup;
    FinalizaPagamento: TToolButton;
    ToolBar1: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton9: TToolButton;
    TotalCartaoPainel: TPanel;
    TotalChequePainel: TPanel;
    TotalPainel: TDBText;
    TotalPainel1: TDBText;
    TotalPrazoPainel: TPanel;
    TrocoPainel: TPanel;
    Valor: TLabel;
    valorEdit: TEdit;
    ValorTotalItens: TDBEdit;
    procedure BuscarFornecedorClick(Sender: TObject);
    procedure BuscarProdutoClick(Sender: TObject);
    procedure cancelarClick(Sender: TObject);
    procedure descricaoEditChange(Sender: TObject);
    procedure InserirActionExecute(Sender: TObject);
    procedure nome_pessoaChange(Sender: TObject);
    procedure SalvarActionExecute(Sender: TObject);
    procedure SalvarButtonClick(Sender: TObject);
    procedure SetDataRecord(Data: TDataRecord); override;
    procedure codigoEditKeyPress(Sender: TObject; var Key: char);
    procedure ControlaBotoes(); override;
    constructor Create(TheOwner: TComponent); override;
    procedure FinalizaPagamentoClick(Sender: TObject);
    procedure GerarDuplicatasPrazoClick(Sender: TObject);
    procedure RemoverItensProdutosClick(Sender: TObject);
  public

  end;

//!Verificar o CFOP, como fazer nos produtos com mais de um CFOP nos produtos

implementation

uses Initializator, MainFr, EntradaDM, MainFm;

{ TEntradaTransRecordFrame }

procedure TEntradaTransRecordFrame.BuscarFornecedorClick(Sender: TObject);
begin
  TheControl.ShowForeignFrame(TMainFrame(FornecedorSearchFrame));
  data_entrada.Text := EntradaData.BuscaData();
end;

procedure TEntradaTransRecordFrame.BuscarProdutoClick(Sender: TObject);
begin
  TheControl.ShowForeignFrame(TMainFrame(ProdutoSearchFrame));
end;

procedure TEntradaTransRecordFrame.cancelarClick(Sender: TObject);
begin
  EntradaData.CancelaPagamentoEntrada(id_entrada.Text);
  RecordPageControl.ActivePage := MasterTabSheet;
  ControlaBotoes();
end;

procedure TEntradaTransRecordFrame.descricaoEditChange(Sender: TObject);
begin
  codigoEdit.SetFocus();
end;

procedure TEntradaTransRecordFrame.InserirActionExecute(Sender: TObject);
begin
  EntradaData.LimpaItens();
  EntradaData.EntradaZQuery.Insert();
  ControlaBotoes();
end;

procedure TEntradaTransRecordFrame.nome_pessoaChange(Sender: TObject);
begin
  data_emissao.SetFocus();
end;

procedure TEntradaTransRecordFrame.SalvarActionExecute(Sender: TObject);
begin
  EntradaData.EntradaZQuery.FieldByName('id_usuario').AsInteger:=MainForm.Usuario;
  inherited;
end;

procedure TEntradaTransRecordFrame.SetDataRecord(Data: TDataRecord);
begin
  codigoEdit.Text := Data.getValue('codigo_barra');
  descricaoEdit.Text := Data.getValue('desc_produto');
  UnidadeEdit.Text:= Data.getValue('sigla_unidade');
  inherited SetDataRecord(Data);
end;

procedure TEntradaTransRecordFrame.codigoEditKeyPress(Sender: TObject; var Key: char);
var
  quant: real;
  valorTotal: real;
begin
  //! Verificar se é 13 para outros SO
  if (key = #13) then begin
    quant := StrToFloatDef(quantidadeEdit.Text, 0);
    valorTotal := StrToFloatDef(valorEdit.Text, 0);
    EntradaData.EntradaAdicionaProduto(codigoEdit.Text, quant, valorTotal);
  end;
end;

procedure TEntradaTransRecordFrame.ControlaBotoes();
var
  I: integer;
begin
  if InTransaction then begin
    RecordToolBar.Enabled := False;
    RecordDBNavigator.Enabled := False;
    for I := 0 to RecordActionList.ActionCount - 1 do begin
      TAction(RecordActionList.Actions[i]).Enabled := False;
    end;
    Gravar.Enabled := True;
    cancelar.Enabled := True;
    FinalizaPagamento.Enabled := False;
    EntradaToolBar.Enabled := True;
    for I := 0 to Self.ComponentCount - 1 do begin
      if Self.Components[I] is TDBLookupComboBox then begin
        TControl(Self.Components[I]).Enabled := True;
      end;
    end;
  end else begin
    inherited ControlaBotoes;
    RecordToolBar.Enabled := True;
    RecordDBNavigator.Enabled := True;
    RecordPageControl.ActivePage := MasterTabSheet;
    FinalizaPagamento.Enabled := AlterarAction.Enabled;
    DetailGroupBox.Enabled := AlterarAction.Enabled;
    BuscarProduto.Enabled := AlterarAction.Enabled;
  end;
end;

constructor TEntradaTransRecordFrame.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  RecordPageControl.ActivePage := MasterTabSheet;
end;

procedure TEntradaTransRecordFrame.FinalizaPagamentoClick(Sender: TObject);
var
  Desconto, Frete, Acrescimo, ValorTotal: real;
begin
  codigoEdit.Text := '';
  descricaoEdit.Text := '';
  Desconto := StrToFloatDef(descontoDBEdit.Text, 0);
  Frete := StrToFloatDef(freteDBEdit.Text, 0);
  Acrescimo := StrToFloatDef(acrescimoDBEdit.Text, 0);
  ValorTotal := StrToFloat(ValorTotalItens.Text) - Desconto + Frete + Acrescimo;
  if (StrToFloat(TotalPainel.Caption) = ValorTotal) then begin
    //Método que vai iniciar a transação
    EntradaData.PagamentoEntrada();
    //Vai mostrar a página de pagamento
    RecordPageControl.ActivePage := FinanceiroTabSheet;
    diaVencimentoEdit.Text := data_entrada.Text;
    ControlaBotoes();
  end else begin
    ShowMessage('Somatorio Itens e Valor Total Nota não Conferem!');
  end;
end;

procedure TEntradaTransRecordFrame.GerarDuplicatasPrazoClick(Sender: TObject);
begin
  if (ComboPagamento.Text = '') or (diaVencimentoEdit.Text = '')then begin
    ShowMessage('Selecione uma condição de Pagamento ou Data');
  end else begin
    EntradaData.GeraDuplicataPagar(StrToInt(id_entrada1.Text), StrToDate(
      diaVencimentoEdit.Text),
      EntradaData.EntradaZQuery.FieldByName('id_fornecedor').AsInteger,
      ComboPagamento.KeyValue,
      EntradaData.EntradaZQuery.FieldByName('valor_total').AsFloat);
  end;
end;

procedure TEntradaTransRecordFrame.SalvarButtonClick(Sender: TObject);
begin
  EntradaData.FechaPagamentoEntrada();
  ControlaBotoes();
  RecordPageControl.ActivePage := MasterTabSheet;
end;

procedure TEntradaTransRecordFrame.RemoverItensProdutosClick(Sender: TObject);
begin
  EntradaData.EntradaRemoveProduto();
end;


initialization
  {$I entradatransrecordfr.lrs}

end.
